package suanfa.datastructure.queue;

public class CycleQueue<T> {
    private T data[];
    private int front,rear;
    int c=10;
    int size;

    public CycleQueue(int c){
        data=(T[])new Object[c];
        front=0;
        rear=0;
        size=0;
    }
    public CycleQueue(){
        data=(T[])new Object[this.c];
        front=0;
        rear=0;
        size=0;
    }

    public boolean isEmpty(){
        return front==rear;
    }
    public int getsize(){
        return this.size;
    }

    public void encapacity(int c){//扩容
        if(c<size){
            return ;
        }
        T[] old=data;
        int j=0;
        data=(T[])new Object[c];
        for(int i=front;i!=rear;i=(i+1)%old.length){
            data[j++]=old[i];
        }
        this.front=0;
        this.rear=j;


    }
    public void enqueue(T d){
        if(this.front==(this.rear+1)%this.data.length){//检查队列
            System.out.println("已满");
            return;
        }
        System.out.println("入队:"+d);
        data[rear]=d;
        this.rear=(rear+1)%data.length;
        size++;
    }
    public T dequeue(){
        if(isEmpty()){
            System.out.println("队列为空");
            return null;
        }


        T d=data[front];
        System.out.println("出队:"+d);
        front=(front+1)% data.length;
        size--;
        return d;
    }


    public static void main(String[] args) {
        CycleQueue<String> c=new CycleQueue<>(10);
        c.enqueue("A");
        c.enqueue("B");
        c.enqueue("C");

        System.out.println("size:"+c.getsize());
        int s=c.getsize();
        for(int i=0;i<s;i++){
            c.dequeue();
        }
        c.enqueue("D");



    }
}
